<?php
namespace Common\Libiary;
class ArrayPage{
    public $totalPage;//全部页数
    public $lists;//每页显示数目
    public $arr = array();//分页的数组
    public $url = '';//分页地址
    public $order;
    public $_error = 'success';//报错提示
    public function __construct(array $arr,$lists = 10,$url,$order =0){
        if( empty($arr) ){
            $this->_error = 'array not found';
            return false;
        }
        if($url == ''){
            $this->_error = 'url not not found';
            return false;
        }
        $this->lists = $lists;
        $this->url = $url;
        $this->arr = $arr;
        $count = count($arr);
        $this->totalPage = ceil($count / $this->lists);
    }

    public function getData(){
        $page = I('page',1);
        $start = ($page - 1)*$this->lists;
        //return $this->lists;
        if($this->order == 1){
            $this->arr = array_reverse($this->arr);
        }
        $res = array_slice($this->arr,$start,$this->lists);
        return $res;
    }

    public function getShow(){
        $page = I('page',1);
        if($page > 1){
            $prevPage = $page-1;
        }else{
            $prevPage = 1;
        }

        if($page < $this->totalPage){
            $nextPage = $page+1;
        }else{
            $page = $this->totalPage;
        }
        $str = '<div style="border:1px; width:100%; height:30px; color:#9999CC";font-size:13px>';
        $str .= "<span>共  {$this->totalPage}  页 / 第 {$page} 页</span>";
        $str .= "<span><a href='{$this->url}&page=1'>   首页  </a></span>";
        if($page == 1){
            $str .= "<span> 上一页  </span>";
        }else{
            $str .= "<span><a href='{$this->url}&page={$prevPage}'> 上一页  </a></span>";
        }


        if($page == $this->totalPage){
            $str .= "<span>下一页  </span>";
        }else{
            $str .= "<span><a href='{$this->url}&page={$nextPage}'>下一页  </a></span>";
        }
        $str .= "<span><a href='{$this->url}&page={$this->totalPage}'>尾页  </a></span>";

        $str .= '<span>转到</span>&nbsp;&nbsp;';
        $str .= '<input type="text" style="width:100px" onkeydown="bindGotoPagenum(event);" name="goto_pagenum" class="txt" value="' . $page . '" />';
        $str .= '<span>页</span>&nbsp;&nbsp;';
        $str .= '<input id="pagenum_button" type="button" class="but btn-info" value="跳转" onclick="location.href=\'' .$this->url.  '&page=' . '\' + document.getElementsByName(\'goto_pagenum\')[0].value " />';

        $str .= '</div>';

        return $str;
    }

    public function getError(){
        return $this->_error;
    }
}


//for($i =0;$i<100;$i++){
//    $arr[$i] = $i*10;
//}
//$page = new ArrayPage($arr,10,'http://localhost/testSvn/arrayPage.php',0);
//$res = $page->getData();
//echo $page->getShow();